package com.ruoyi.buss.excelnfo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.buss.excelnfo.service.ExprotToExcelService;
import com.ruoyi.buss.task.domain.DataPlanTaskRelation;
import com.ruoyi.buss.task.domain.DataTaskInfo;
import com.ruoyi.buss.task.domain.DataUserTaskRelation;
import com.ruoyi.buss.task.domain.UserTaskRelationDO;
import com.ruoyi.buss.task.mapper.DataPlanTaskRelationMapper;
import com.ruoyi.buss.task.mapper.DataTaskInfoMapper;
import com.ruoyi.buss.task.mapper.DataUserTaskRelationMapper;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.buss.utils.GetOnlyId;
import com.ruoyi.common.core.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


/**
 * 专业市场客户Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class ExprotToExcelServiceImpl implements ExprotToExcelService
{
    @Autowired
    private DataPlanTaskRelationMapper dataPlanTaskRelationMapper;

    @Autowired
    private DataTaskInfoMapper dataTaskInfoMapper;

    @Autowired
    private DataUserTaskRelationMapper dataUserTaskRelationMapper;

    @Override
    @Transactional
    public AjaxResult importTaskInfoExces(MultipartFile file, String taskName, LoginUser userInfo , String beginDate, String endDate,List<DataUserTaskRelation> userList){
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        //获取前端传过来的任务名称,若任务名称为空,则取前端穿的文件名
        if (StringUtils.isEmpty(taskName)) {
            taskName = file.getOriginalFilename();
            if (taskName.endsWith(".xls")) {
                taskName = taskName.substring(0, taskName.length() - 4);
            } else if (taskName.endsWith(".xlsx")) {
                taskName = taskName.substring(0, taskName.length() - 5);
            }
        }
        //任务类型  1个人 0 总行
        String taskType = "0";
        if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
            taskType = "0";
        } else if (userInfo.getRoles().contains("zhhz")||userInfo.getRoles().contains("common")) {
            taskType = "1";
        } else {
            throw new CustomException("没有权限进行该操作!");
        }
        String taskId = GetOnlyId.getOnlyId();
        // 向DATA_PLAN_TASK_RELATION总行营销计划关联表插入一条记录
        DataPlanTaskRelation dataPlanTaskRelation= new DataPlanTaskRelation();

        dataPlanTaskRelation.setTaskId(taskId);
        dataPlanTaskRelation.setOrgId(orgId);
        dataPlanTaskRelation.setUserId(userId);
        dataPlanTaskRelation.setStatus("0");
        dataPlanTaskRelation.setPlanCode("510591589207244800");
        dataPlanTaskRelationMapper.insertDataPlanTaskRelation(dataPlanTaskRelation);

        // 向DATA_TASK_INFO营销任务表中插入一条任务
        DataTaskInfo dataTaskInfo = new DataTaskInfo();

        dataTaskInfo.setTaskId(taskId);
        dataTaskInfo.setTaskName(taskName);
        dataTaskInfo.setUpdateUser(userId);

        // status 状态 0不正常 1正常
        dataTaskInfo.setStatus("1");

        dataTaskInfo.setBeginDate(DateUtilss.beginOfDate(DateUtilss.strToDate(beginDate)));
        dataTaskInfo.setEndDate(DateUtilss.endOfDate(DateUtilss.strToDate(endDate)));
        dataTaskInfo.setCreateTime(DateUtils.getNowDate());
        // complete 营销状态 0未完成 1已完成
        dataTaskInfo.setComplete("0");
        dataTaskInfo.setCreateUser(userId);
        dataTaskInfo.setUserId(userId);
        dataTaskInfo.setOrgId(orgId);
        // 目标客户类型（0群体，1自选）
        dataTaskInfo.setIfGroup("0");
        // 营销任务类型(0总行，1个人)
        dataTaskInfo.setTaskType(taskType);
        // 营销目标
        dataTaskInfo.setTaskTarget("CK,DK,ETC,XYK,SJYH,WY,DXQY,SDDJ,SYB,POS");
        dataTaskInfo.setFinishNum(0);
        dataTaskInfoMapper.insertDataTaskInfo(dataTaskInfo);

        // 向DATA_USER_TASK_RELATION营销任务和客户关系表插入数据
        for(DataUserTaskRelation dataUserTaskRelation:userList){
            String id = GetOnlyId.getOnlyId();
            dataUserTaskRelation.setId(id);
            dataUserTaskRelation.setTaskId(taskId);
            dataUserTaskRelation.setCreateTime(DateUtils.getNowDate());
            dataUserTaskRelation.setBeginDate(DateUtilss.beginOfDate(DateUtilss.strToDate(beginDate)));
            dataUserTaskRelation.setEndDate(DateUtilss.endOfDate(DateUtilss.strToDate(endDate)));
            dataUserTaskRelation.setStatus("1");
            dataUserTaskRelation.setComplete("0");
            if("".equals(dataUserTaskRelation.getType()) || dataUserTaskRelation.getType() == null || " ".equals(dataUserTaskRelation.getType())){
                dataUserTaskRelation.setType("1");
            }
            dataUserTaskRelationMapper.insertDataUserTaskRelation(dataUserTaskRelation);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult importUserTaskExcel(MultipartFile file, LoginUser userInfo, List<UserTaskRelationDO> userList) {
        if(userList !=null && userList.size() > 0 ) {
            for(UserTaskRelationDO po: userList) {
                DataUserTaskRelation param = new DataUserTaskRelation();
                param.setOrgId(po.getOrgId());
                param.setUserId(po.getUserId());
                DataUserTaskRelation dataUserTaskRelations = dataUserTaskRelationMapper.selectDataUserTaskRelationById(po.getId());
                param.setIdCard(dataUserTaskRelations.getIdCard());
                param.setTaskId(dataUserTaskRelations.getTaskId());
                dataUserTaskRelationMapper.updateDataUserTaskRelationByIdcard(param);
            }
        }
        return AjaxResult.success();
    }
}
